db = require '../utils/connect'
async = require 'async'
request = require 'request'
Article = require '../models/article'
moment = require 'moment'
url = require 'url'
querystring = require 'querystring'

module.exports = 
	'get /admin/accounts': (req, res)->
		page = req.query.page || 0
		local = {}
		async.series [
				(cb)->
					sql = "select * from accounts  order by  active desc, approved_count  desc, article_count desc limit ?,200"
					db.runSql sql , [200*page], (err, result)->
						local.accounts= result
						cb err
				(cb)->
					sql = "select count(*) as count from accounts "
					db.queryOne sql, (err, result)->
						local.count = result.count
						cb err
			],(err)->
				log.error err if err 
				res.send local

	'post /admin/cookie': (req, res)->
		cookie = req.body.cookie
		sql = "update cookie set cookie = ? where id = 1 "
		db.runSql sql, [cookie],  (err, result)->
			log.error err if err 
			res.send result

	'post /admin/accounts/:id/update':(req, res)->
		qiniu = req.app.qiniu
		headimg = new qiniu.Bucket('headimg')
		local = {}
		async.series [
			(cb)->
				if req.body.category
					sql = "update accounts set category = ? where id= ?"
					db.runSql sql, [req.body.category,req.params.id], cb
				else
					cb null
			(cb)->
				sql = 'select headimgurl,username from accounts where id = ?'
				db.queryOne sql, [req.params.id], (err, result)->
					local.account = result
					cb err
			(cb)->
				sql = "select cookie from cookie where id = 1"
				db.queryOne sql,  (err, result)->
					local.cookie = result.cookie
					cb err
			(cb)->
				ps = headimg.createPutStream(local.account.username + '.jpg')
				jar = request.jar()
				ps.on 'error',(err)->
					local.err = err
				ps.on 'end',(result)-> 
					cb null
				cookies = local.cookie
				for a in cookies.split ';'
					cookie = request.cookie a
					jar.add(cookie)
				url = 'http://wx.qq.com'+local.account.headimgurl
				request({url,jar}).pipe ps
		],(err)->
			log.error err if err 
			res.send 200,local

	'get /admin/accounts/:id/delete':(req, res)->
		async.series [
				(cb)->
					sql = "insert into accounts_delete select * from accounts where id = ?"
					db.runSql sql, [req.params.id], cb
				(cb)->
					sql = "delete from accounts where id = ?"
					db.runSql sql, [req.params.id], cb
			],(err)->
				log.error err if err 
				res.send 200, err

	'get /admin/accounts/:id/active':(req, res)->
		active = req.query.active || 0
		active = Math.abs active-1
		async.series [
				(cb)->
					sql = "update accounts set active = ? where id = ? "
					db.runSql sql, [active, req.params.id], cb
			],(err)->
				log.error err if err 
				res.send 200, err


	'get /admin/articles': (req, res)->
		page = req.query.page || 0
		local = {}
		async.series [
				(cb)->
					sql = "select id,title,cover,digest,nickname,contenturl,datetime from articles where state = 0 order by datetime desc limit ?,50 "
					db.runSql sql , [50*page], (err, result)->
						local.articles= result
						cb err
				(cb)->
					sql = "select count(*) as count from articles "
					db.queryOne sql, (err, result)->
						local.count = result.count
						cb err
			],(err)->
				log.error err if err 
				local.articles.forEach (item)->
					item.datetime = moment(item.datetime).fromNow()
				res.send local

	'get /admin/articles/:id/complete':(req, res)->
		Article.complete req.params.id, (err)->
			log.error err if err 
			res.send err

	'get /admin/articles/:id/delete':(req, res)->
		async.series [
				(cb)->
					sql = "update articles set state = -1 where id = ?"
					db.runSql sql, [req.params.id], cb
			],(err)->
				log.error err if err 
				res.send 200, err

	'get /admin/articles/:id/approve':(req, res)->
		Article.approve req.params.id,(err)->
			log.error err if err 
			res.send 200, err

	'get /admin/index' : (req, res)->
		res.render 'admin/admin'

	'get /admin/history/:uin' : (req, res)->
		db.queryOne "select uin,`key` from cookie", (err, result)->
			log.error err if err 
			biz = new Buffer(req.params.uin.toString()).toString('base64')
			res.redirect 'http://mp.weixin.qq.com/mp/getmasssendmsg?__biz='+biz+'&uin='+result.uin+'&key='+result.key+'&frommsgid=-1&count=100'


	'get /admin/count/update':(req, res)->
		async.series [
			(cb)->
				sql = "update accounts, (SELECT count(*) as count, username FROM zi.articles_approved group by username) as t set accounts.approved_count = t.count where accounts.username = t.username"
				db.runSql sql, cb
			(cb)->
				sql = "update accounts, (SELECT count(*) as count, fakeid FROM zi.appmsgs group by fakeid) as t  set accounts.article_count = t.count where accounts.uin = t.fakeid"
				db.runSql sql, cb
		],(err)->
			log.error err if err 
			res.send err

	'get /admin/articles/count':(req, res)->
		local = {}
		async.series [
			(cb)->
				sql = "select count(*) as count from articles "
				db.queryOne sql, (err,result)->
					local.articles_count = result.count
					cb err
			(cb)->
				sql = "select count(*) as count from articles_approved"
				db.queryOne sql, (err, result)->
					local.approved_count = result.count
					cb err
		],(err)->
			log.error err if err 
			res.send local

	'get /key':(req, res)->
		res.render 'admin/key'

	'post /key':(req, res)->
		href = decodeURIComponent req.body.key
		query =  querystring.parse url.parse(href).query
		uin = query.uin
		key = query.key
		db.runSql "update cookie set uin=?, `key`=? where id = 1", [uin,key],(err)->
			res.send arguments